import { config, loadCssRemote, loadRemote } from '@vimi/utils-tool'

const lfCdn = {
  baseUrl: '/libs/logicflow/0.2.9',
  js: '/core/logic-flow.js',
  css: '/core/style/index.css',
  exCss: '/extension/style/index.css',
}

/**
 * 加载logicFlow
 *
 * 插件包括 BpmnElement Control DndPanel Menu RectLabelNode ResizeNode SelectionSelect Snapshot
 *
 * @param {string[]} extensions 插件
 * @returns Promise
 */
export const loadLogicFlow = (extensions = []) => {
  const baseUrl = `${config.baseUrl}${lfCdn.baseUrl}`
  loadCssRemote(`${baseUrl}${lfCdn.css}`)
  if (extensions.length) {
    loadCssRemote(`${baseUrl}${lfCdn.exCss}`)
  }
  return Promise.all([
    loadRemote(`${baseUrl}${lfCdn.js}`, 'LogicFlow'),
    ...extensions.map(it => loadRemote(`${baseUrl}/extension/${it}.js`, it)),
  ]).then(() => {
    extensions.forEach(it => window.LogicFlow.use(window[it]))
  })
}
